<!--
 * @Author: 可以清心
 * @Description: 
 * @Date: 2023-03-08 18:32:41
 * @LastEditTime: 2023-03-08 19:20:33
-->
<template>
    <div class="loading-wrap">
        <div class='app-loading' style="color: #005EBA;">
            <div class="loading">
                <div></div>
                <div></div>
                <div></div>
                <div></div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapGetters } from 'vuex'
import axios from 'axios'
import COMMON_PASSWORD from './config.js'
import encryption from '@/utils/encryption.util.js'
import storage from '@/utils/storage'
export default {
    name: 'SsoPage',
    data() {
        return {

        }
    },
    computed: {
        ...mapGetters('userStore', ['userData'])
    },
    mounted(){
        this.encryptionPassWord();
    },
    methods: {
        /**
         * @description: 获取应用信息
         * @return {any}
         */
        getApplicationInfo() {
            let GLOBAL_ENV = window.GLOBAL_ENV;

            if (GLOBAL_ENV) {
                return {
                    tenantId: GLOBAL_ENV['VUE_APP_TENANT_ID'],
                    appId: GLOBAL_ENV['VUE_APP_APP_ID'],
                    domain: GLOBAL_ENV['VUE_APP_BASE_DOMAIN'],
                    path: GLOBAL_ENV['VUE_APP_PUBLIC_PATH']
                }
            }

            return {}
        },
        async encryptionPassWord() {
            const { getApplicationInfo } = this;
            let encyptionPass = await encryption.passwordEncryption(COMMON_PASSWORD);
            let applicationInfo = getApplicationInfo();

            let tenantId = applicationInfo.tenantId || '332938032653008897';
            let appId = applicationInfo.appId || '336863401667985408';

            let domain = applicationInfo.domain || location.host;
            let username = this.userData.uid;
            let path = '/m/'

            let url = '';

            if (location.hostname !== 'localhost') {
                url = '/xdap-app/app/login'
            } else {
                url = 'http://172.19.11.93:30615/xdap-app/app/login'
            }

            axios({
                url,
                method: 'post',
                data: {
                    type: "account",
                    tenantId,
                    securityCode: '',
                    password: encyptionPass,
                    appId,
                    account: username
                }
            }).then((resp) => {
                const res = resp.data;
                if (res.code === 'ok') {
                    const { setModule } = storage;
                    const { data } = res;

                    setModule(data);

                    if (location.hostname !== 'localhost') {
                        window.open(domain + path, '_self')
                    }
                } else {
                    Toast.fail('登录失败');
                    this.loading = false;
                }
            }).catch(() => {
                Toast.fail('您无此应用权限');
                this.loading = false;
            })
        },
    }
}
</script>

<style>
@import "../../assets/css/loading.css";

html {
    font-size: 10px;
}

.sso {
    height: 100%;
    width: 100%;
}

.loading-wrap {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>